#####################################################################
# Product:      KnowledgeEngine (Plone Edition)
#
# File:         TextEditlet.py
#
# Copyright:    Copyright (c) 2004, Faulkner Technologies
#
# Author:       Brian Oliver
#
# Description:  This file defines the TextEditlet class, that is
#               used to generate text editlets for property editors.
#
# See Also:     PropertyEditlet, PropertyRefleciton, 
#               PropertyDefinition, PropertyAccess 
#####################################################################

#####################################################################
## KnowledgeEngine Library Imports
from Products.KnowledgeEngine.Common import *
from Products.KnowledgeEngine.Core import *
from Products.KnowledgeEngine.Editlet import Editlet
from Products.KnowledgeEngine.PropertyTypes import *
from Products.KnowledgeEngine.PropertyFormats import *
from Products.KnowledgeEngine.PropertyStructures import *

#####################################################################
## Zope Library Imports
# (none)
 
#####################################################################
## Python Library Imports
# (none)

#####################################################################
## TextEditlet Class Definition
class TextEditlet(Editlet):
    """
        An editlet for editing text properties  
    """
    
    #---------------------------------------------------------------#
    
    def isEditable(self, propertyDefinition, propertyAccess=None, value=None, isForConstructor=false, REQUEST=None, context=None):
        """
            Returns if the specified propertyDefinition can be
            edited by this editlet
        """

        return propertyDefinition.getType() == pt_STRING and propertyDefinition.getStructure() == ps_ATOMIC and (propertyDefinition.getFormat() == pf_NATIVE or propertyDefinition.getFormat() == pf_TEXT_SINGLELINE or propertyDefinition.getFormat() == pf_TEXT_MULTILINE) and not propertyDefinition.hasEnumeratedValues()
        
    #---------------------------------------------------------------#

    def generateEditorFor(self, propertyDefinition, propertyAccess=None, value=None, isForConstructor=false, REQUEST=None, context=None):
        """
            Returns a HTML editlet for the specified property.
        """

        #the resulting editlet
        result = ""

        #-----------------
        #is the property read only?        
        if propertyDefinition.isReadPermitted() and not propertyDefinition.isWritePermitted():

            #get the value for the property
            if isForConstructor:
                value = propertyDefinition.getDefaultValue()
            else:
                value = propertyAccess.getPropertyValue(propertyDefinition.getIdentity())

            #markup multi-line text and non-html as necessary
            if propertyDefinition.getFormat() != pf_HTML and value != None:
                value = toHTML(value)
            
            #create the editor
            result = "<table border=1><tr><td>%s</td></tr></table>" % (value,)
        
        #-----------------
        #is the property writeable?
        if propertyDefinition.isWritePermitted():
       
            if propertyDefinition.isReadPermitted():
                #get the value for the property
                if isForConstructor:
                    value = propertyDefinition.getDefaultValue()
                else:
                    #get the value for the property
                    value = propertyAccess.getPropertyValue(propertyDefinition.getIdentity())                                                    
            else:
                #not readable so the default is empty
                value = ""
                
            #is the property single or multiline?
            if propertyDefinition.getFormat() == pf_TEXT_SINGLELINE:
                #create the editor
                result = "<input type=\"text\" id=\"ke%s\" name=\"ke%s\" size=\"%s\" value=\"%s\">" % (propertyDefinition.getIdentity(), propertyDefinition.getIdentity(), 40, toHTML(value))           
            else:

                #create the editor
                result = "<textarea id=\"ke%s\" name=\"ke%s\" cols=\"%s\" rows=\"%s\">%s</textarea>" % (propertyDefinition.getIdentity(), propertyDefinition.getIdentity(), 40, 6, value)           
                        
        return result
        
    #---------------------------------------------------------------#

    def updatePropertyWith(self, propertyDefinition, propertyAccess, REQUEST=None):
        """
            Attempts to update the specified property on the
            object implementing the PropertyAccess interface with the
            specified HTTP REQUEST.
        """

        #is write permitted on the specified property?
        if propertyDefinition.isWritePermitted():

            # Check we have a value if it isMandatory
            propertyDefinition.verifyMandatoryValue(REQUEST["ke%s" % (propertyDefinition.getIdentity(),)])
        
            #set the value for the property from the REQUEST
            propertyAccess.setPropertyValue(propertyDefinition.getIdentity(), REQUEST["ke%s" % (propertyDefinition.getIdentity(),)])
        
    #---------------------------------------------------------------#

#####################################################################
## Class Initialisation

# Register Editlet Information with the Knowledge Engine
registerEditlet("TextEditlet", TextEditlet())

#####################################################################
